﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;

namespace Microsoft.JCI.Common.Utility
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)]
    public class EnumeratorHelper : Attribute
    {
         private string enumDisplayText;
        private int enumRank;
        private FieldInfo fieldIno;
       
        public EnumeratorHelper(string enumDisplayText, int enumRank)
        {
            this.enumDisplayText = enumDisplayText;
            this.enumRank = enumRank;
        }


        public EnumeratorHelper(string enumDisplayText)
            : this(enumDisplayText, 5) { }

        public string EnumDisplayText
        {
            get { return this.enumDisplayText; }
        }

        public int EnumRank
        {
            get { return enumRank; }
        }

        public int EnumValue
        {
            get { return (int)fieldIno.GetValue(null); }
        }

        public string FieldName
        {
            get { return fieldIno.Name; }
        }

        #region  =========================================对枚举描述属性的解释相关函数

        /// <summary>
        /// 排序类型
        /// </summary>
        public enum SortType
        {
            /// <summary>
            ///按枚举顺序默认排序
            /// </summary>
            Default,
            /// <summary>
            /// 按描述值排序
            /// </summary>
            DisplayText,
            /// <summary>
            /// 按排序
            /// </summary>
            Rank
        }

        private static System.Collections.Hashtable cachedEnum = new Hashtable();

               
        public static string GetEnumText(Type enumType)
        {
            EnumeratorHelper[] eds = (EnumeratorHelper[])enumType.GetCustomAttributes(typeof(EnumeratorHelper), false);
            if (eds.Length != 1) return string.Empty;
            return eds[0].EnumDisplayText;
        }
               
        public static string GetFieldText(object enumValue)
        {
            EnumeratorHelper[] descriptions = GetFieldTexts(enumValue.GetType(), SortType.Default);
            foreach (EnumeratorHelper ed in descriptions)
            {
                if (ed.fieldIno.Name == enumValue.ToString()) return ed.EnumDisplayText;
            }
            return string.Empty;
        }
      
        public static EnumeratorHelper[] GetFieldTexts(Type enumType)
        {
            return GetFieldTexts(enumType, SortType.Default);
        }

      
        public static EnumeratorHelper[] GetFieldTexts(Type enumType, SortType sortType)
        {
            EnumeratorHelper[] descriptions = null;
            
            if (cachedEnum.Contains(enumType.FullName) == false)
            {
                FieldInfo[] fields = enumType.GetFields();
                ArrayList edAL = new ArrayList();
                foreach (FieldInfo fi in fields)
                {
                    object[] eds = fi.GetCustomAttributes(typeof(EnumeratorHelper), false);
                    if (eds.Length != 1) continue;
                    ((EnumeratorHelper)eds[0]).fieldIno = fi;
                    edAL.Add(eds[0]);
                }

                cachedEnum.Add(enumType.FullName, (EnumeratorHelper[])edAL.ToArray(typeof(EnumeratorHelper)));
            }
            descriptions = (EnumeratorHelper[])cachedEnum[enumType.FullName];
            if (descriptions.Length <= 0) throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");

            //按指定的属性冒泡排序
            for (int m = 0; m < descriptions.Length; m++)
            {
                //默认就不排序了
                if (sortType == SortType.Default) break;

                for (int n = m; n < descriptions.Length; n++)
                {
                    EnumeratorHelper temp;
                    bool swap = false;

                    switch (sortType)
                    {
                        case SortType.Default:
                            break;
                        case SortType.DisplayText:
                            if (string.Compare(descriptions[m].EnumDisplayText, descriptions[n].EnumDisplayText) > 0) swap = true;
                            break;
                        case SortType.Rank:
                            if (descriptions[m].EnumRank > descriptions[n].EnumRank) swap = true;
                            break;
                    }

                    if (swap)
                    {
                        temp = descriptions[m];
                        descriptions[m] = descriptions[n];
                        descriptions[n] = temp;
                    }
                }
            }

            return descriptions;
        }

        #endregion
    }
}
